package com.yds.ydsdatasetbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yds.ydsdatasetbackend.annotation.AuthCheck;
import com.yds.ydsdatasetbackend.common.BaseResponse;
import com.yds.ydsdatasetbackend.common.DeleteRequest;
import com.yds.ydsdatasetbackend.common.ResultUtils;
import com.yds.ydsdatasetbackend.constant.UserConstant;
import com.yds.ydsdatasetbackend.exception.BusinessException;
import com.yds.ydsdatasetbackend.exception.ErrorCode;
import com.yds.ydsdatasetbackend.exception.ThrowUtils;
import com.yds.ydsdatasetbackend.model.dto.file.FileEditRequest;
import com.yds.ydsdatasetbackend.model.dto.file.FileQueryRequest;
import com.yds.ydsdatasetbackend.model.dto.file.train.FileTrainAddRequest;
import com.yds.ydsdatasetbackend.model.dto.file.train.FileTrainEditRequest;
import com.yds.ydsdatasetbackend.model.dto.file.train.FileTrainQueryRequest;
import com.yds.ydsdatasetbackend.model.dto.file.variables.FileVariablesQueryRequest;
import com.yds.ydsdatasetbackend.model.dto.file.variables.FileVariablesUpdateRequest;
import com.yds.ydsdatasetbackend.model.entity.File;
import com.yds.ydsdatasetbackend.model.entity.FileTrain;
import com.yds.ydsdatasetbackend.model.entity.FileVariables;
import com.yds.ydsdatasetbackend.model.entity.User;
import com.yds.ydsdatasetbackend.model.enums.FileFillEnum;
import com.yds.ydsdatasetbackend.model.vo.FileTrainVO;
import com.yds.ydsdatasetbackend.model.vo.FileVO;
import com.yds.ydsdatasetbackend.model.vo.FileVariablesVO;
import com.yds.ydsdatasetbackend.service.FileTrainService;
import com.yds.ydsdatasetbackend.service.FileVariablesService;
import com.yds.ydsdatasetbackend.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/file/train")
public class FileTrainController {

    @Resource
    private FileTrainService fileTrainService;

    @Resource
    private UserService userService;


    /**
     * 更新图片（仅管理员可用）
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.USER_LOGIN_STATE)
    public BaseResponse<FileTrainVO> addFileTrain(@RequestBody FileTrainAddRequest fileTrainAddRequest, HttpServletRequest request) {
        if (fileTrainAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(fileTrainService.addFileTrain(fileTrainAddRequest, loginUser));
    }

    /**
     * 编辑文件
     */
    @PostMapping("/edit")
    @AuthCheck(mustRole = UserConstant.USER_LOGIN_STATE)
    public BaseResponse<Boolean> editFileTrain(@RequestBody FileTrainEditRequest fileTrainEditRequest, HttpServletRequest request) {
        if (fileTrainEditRequest == null || fileTrainEditRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        fileTrainService.editFile(fileTrainEditRequest, loginUser);
        return ResultUtils.success(true);
    }

    /**
     * 删除文件
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.USER_LOGIN_STATE)
    public BaseResponse<Boolean> deleteFileTrain(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        fileTrainService.deleteFileTrain(deleteRequest.getId(), loginUser);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取文件（仅管理员可用）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<FileTrain> getFileTrainById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        FileTrain fileTrain = fileTrainService.getById(id);
        ThrowUtils.throwIf(fileTrain == null, ErrorCode.NOT_FOUND_ERROR);
        // 获取封装类
        return ResultUtils.success(fileTrain);
    }

    /**
     * 根据 id 获取文件（封装类）
     */
    @GetMapping("/get/vo")
    @AuthCheck(mustRole = UserConstant.USER_LOGIN_STATE)
    public BaseResponse<FileTrainVO> getFileTrainVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        FileTrain fileTrain = fileTrainService.getById(id);
        ThrowUtils.throwIf(fileTrain == null, ErrorCode.NOT_FOUND_ERROR);
        FileTrainVO fileTrainVO = fileTrainService.getFileTrainVO(fileTrain, request);
//        fileVO.setPermissionList(permissionList);
        // 获取封装类
        return ResultUtils.success(fileTrainVO);
    }

    /**
     * 分页获取文件列表（仅管理员可用）
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<FileTrain>> listFileTrainByPage(@RequestBody FileTrainQueryRequest fileTrainQueryRequest, HttpServletRequest request) {

        long current = fileTrainQueryRequest.getCurrent();
        long size = fileTrainQueryRequest.getPageSize();
        // 查询数据库
        Page<FileTrain> fileTrainPage = fileTrainService.page(new Page<>(current, size),
                fileTrainService.getQueryWrapper(fileTrainQueryRequest));
        return ResultUtils.success(fileTrainPage);
    }

    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.USER_LOGIN_STATE)
    public BaseResponse<Page<FileTrainVO>> listFileTrainVOByPage(@RequestBody FileTrainQueryRequest fileTrainQueryRequest,
                                                       HttpServletRequest request) {
        long current = fileTrainQueryRequest.getCurrent();
        long size = fileTrainQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);

        // 查询数据库
        Page<FileTrain> fileTrainPage = fileTrainService.page(new Page<>(current, size),
                fileTrainService.getQueryWrapper(fileTrainQueryRequest));
        // 获取封装类
        return ResultUtils.success(fileTrainService.getFileTrainVOPage(fileTrainPage, request));
    }

}
